package kr.hkit.password.fragment.item;

import kr.hkit.password.R;
import kr.hkit.password.dao.InfoDao;
import kr.hkit.password.fragment.account.AccountView;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class ItemWebView extends Fragment {
	private WebView wv;
	private String url;
	private InfoDao info;
	private static final String TAG = "PW";
	private ProgressDialog mProg;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Log.d(TAG, "WebViewFrag onCreateView");
		View view = inflater.inflate(R.layout.webview_frag, container, false);
		info = (InfoDao) getActivity().getApplication();
		info.setMain(false);
		wv = (WebView) view.findViewById(R.id.webView);
		wv.setWebViewClient(new MyWebView());
		WebSettings set = wv.getSettings();
		set.setJavaScriptEnabled(true);
		set.setBuiltInZoomControls(true);
		
		if (getArguments() == null) {
			url = "http://www.google.com";
		} else {
			url = getArguments().getString("url");
		}
		wv.loadUrl(url);

		return view;
	}
	
	public class MyWebView extends WebViewClient {

		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			view.loadUrl(url);
			return true;
		}

		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			super.onPageStarted(view, url, favicon);
			Log.d(TAG, "MyWebView onPageStarted");
			if (mProg == null) {
				mProg = new ProgressDialog(getActivity());
				mProg.setMessage("로딩중... 잠시만 기다려주세요.");
				mProg.setCancelable(false);
				mProg.show();
			}
		}
		
		@Override
		public void onPageFinished(WebView view, String url) {
			super.onPageFinished(view, url);
			Log.d(TAG, "MyWebView onPageFinished");
			if (mProg != null && mProg.isShowing()) {
				mProg.dismiss();
				mProg = null;
			}
		}
	}
	
	@Override
	public void onDestroyView() {
		super.onDestroyView();
		Log.d(TAG, "WebViewFrag onDestroyView()");
		info.setUrl(wv.getUrl());
	}
}


